﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OntologyUtility;

namespace OntologyFormDesigner.GuiComponent
{
    public partial class ItemsChoose : WizardPanel
    {
        public ItemsChoose(WizardForm wizard, Control control) : base(control)
        {
            InitializeComponent();
            this.Parent = wizard;
            //this.PageComplete = true;
            //this.canFlipToNextPage();
            wizard.Back();
        }

        private void textModify(object sender, EventArgs e) 
        {
            ((Node)((TextBox)sender).Tag).Alias = ((TextBox)sender).Text;
        }

        private void textModifyRootNode(object sender, EventArgs e)
        {
            ((Node)((TextBox)sender).Tag).Alias = ((TextBox)sender).Text;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Sei sicuro di voler eliminare questo item?", "Questions", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                nodes.Sort(Node.CompareNodeByWeight);
                
                Node node = (Node)((Button)sender).Tag;
                nodes.Remove(node);

                for (int i = 0; i < nodes.Count; i++)
                    ((Node)nodes[i]).Weight = (i + 1);
                //tPanel.Controls.Container.reRemoveAt(tPanel.GetRow((Button)sender));
                this.buildTable();
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            try
            {
                Node node2Down = ((Node)((Button)sender).Tag);
                Node node2Up = 
                    nodes.Find(
                        delegate(Node _node)
                        {
                            return (_node.Weight == (node2Down.Weight + 1));
                        }
                );

                node2Up.Weight -= 1;
                node2Down.Weight += 1;

                nodes.Sort(Node.CompareNodeByWeight);

                this.buildTable();
            }
            catch (Exception ex) 
            {
                showMessage("Si è verificato un errore: " + ex);
            }
        }

        private void btnUp_Click(object sender, EventArgs e) //devo diminuire il peso di quello selezionato e aumentare il peso di quello che sta sopra
        {
            try {
                Node node2Up = ((Node)((Button)sender).Tag);
                Node node2Down = nodes.Find(
                     delegate(Node _node)
                     {
                         return (_node.Weight == (node2Up.Weight - 1));
                     }
                );

                node2Up.Weight -= 1;
                node2Down.Weight += 1;

                nodes.Sort(Node.CompareNodeByWeight);

                this.buildTable();
                }
            catch (Exception ex)
            {
                Console.WriteLine("ItemChoose.cs - btnUp_Click ex: {0}", ex.ToString());
                //showMessage("Si è verificato un errore: " + ex);
            }
        }

        public override void Next()
        {
            try
            {
                Element element = (Element)Parent.Tag;

                if (PreviousPage.ObjectWizard is Widget.ListBox)
                {
                    Widget.ListBox widget = (Widget.ListBox) PreviousPage.ObjectWizard;
                    widget.update(nodes);
                    widget.OntologiesNodes = new OntologyUtility.Xml.OntologiesNodes(nodes);
                    widget.Text = ((Node)widget.Node).Alias;
                }
                else if (PreviousPage.ObjectWizard is Widget.ComboBox)
                {
                    Widget.ComboBox widget = (Widget.ComboBox)PreviousPage.ObjectWizard;
                    widget.update(nodes);
                    widget.OntologiesNodes = new OntologyUtility.Xml.OntologiesNodes(nodes);
                    widget.Text = ((Node)widget.Node).Alias;
                }
                else if (PreviousPage.ObjectWizard is Widget.RadioButtonGroupBox)
                {
                    Widget.RadioButtonGroupBox widget = (Widget.RadioButtonGroupBox)PreviousPage.ObjectWizard;
                    widget.update(nodes);
                    widget.OntologiesNodes = new OntologyUtility.Xml.OntologiesNodes(nodes);
                    widget.Text = ((Node)widget.Node).Alias;
                }
                else if (PreviousPage.ObjectWizard is Widget.Slider)
                {
                    Widget.Slider widget = (Widget.Slider)PreviousPage.ObjectWizard;
                    widget.update(nodes);
                    widget.Text = ((Node)widget.Node).Alias;
                    widget.OntologiesNodes = new OntologyUtility.Xml.OntologiesNodes(nodes);
                }
                else if (PreviousPage.ObjectWizard is Widget.CheckButtonGroupBox)
                {
                    Widget.CheckButtonGroupBox widget = (Widget.CheckButtonGroupBox)PreviousPage.ObjectWizard;
                    widget.update(nodes);
                    widget.OntologiesNodes = new OntologyUtility.Xml.OntologiesNodes(nodes);
                    widget.Text = ((Node)widget.Node).Alias;
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("ItemChoose.cs " + ex.ToString());
            }
        }
        
    }
}
